package com.sx.pipeline.ui.group

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.sx.pipeline.R
import com.sx.pipeline.adapter.GroupManagerAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.arouth.RoutePath.Chat.CHAT_GROUP_LIST
import com.sx.pipeline.databinding.ActGroupListBinding
import com.sx.pipeline.databinding.ActGroupManagerBinding
import com.sx.pipeline.repos.http.res.GroupBean
import io.rong.imkit.userinfo.RongUserInfoManager

@Route(path = CHAT_GROUP_LIST)
class GroupListAct: CYDataBindingBaseActivity<ActGroupListBinding, GroupManagerVm>(R.layout.act_group_list) {
    @Autowired
    @JvmField
    var id: String =""
    var adapter= GroupManagerAdapter()
    override fun initViewModel() {
        super.initViewModel()
        mViewModel.groupLiveListData.observe(this){
            it?.map { GroupBean(it) }?.toMutableList()?.let {
                if(mViewModel.hasAdmin){
                    //有管理人权限
                    it.add(GroupBean(tempType = 1))
                    it.add(GroupBean(tempType = 2))
                }
                adapter.setList(it)
            }
        }
    }
    override fun initData() {
        super.initData()
        mBinding.list.layoutManager = GridLayoutManager(this, 5)
        mBinding.list.adapter = adapter
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarColor(R.color.color_0188ff)
            .statusBarDarkFont(false)
            .init()
        mBinding.list.layoutManager = GridLayoutManager(this, 5)
        mBinding.list.adapter = adapter
        mViewModel.groupInfoById(id)
        mBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                super.onLeftClick(titleBar)
                finish()
            }
        })
        setTitle()
        adapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as GroupBean
            if (bean.itemType == 0) {
                //详情
                bean.contact?.userUnitId?.let {
                    ARouter.getInstance().build(RoutePath.Chat.CHAT_PERSONAL)
                        .withInt(IntentKey.ID, it).navigation()
                }
            } else {
                ARouter.getInstance().build(RoutePath.Chat.CHAT_GROUP_OPTION)
                    .withInt("optionType", bean.itemType)
                    .withString("id", id)
                    .navigation()
            }
        }
    }

    private fun setTitle() {
        val group = RongUserInfoManager.getInstance().getGroupInfo(id)
        if(group!=null){
            mBinding.titleBar.title=group.name
        }
    }
}